984B - Minesweeper - CodeForces Solution


implementation *1100

Please click on ads to support us..

Python Code:

line = input().split()
n, m = int(line[0]), int(line[-1])
 
grid = []
 
for i in range(n):
    grid.append(input())
    
def k_neighbors(k, arr, x, y):
    total = 0
 
    for i in range(x - 1, x + 2):
        for j in range(y - 1, y + 2):
            if (0 <= i < len(arr)) and (0 <= j < len(arr[i])):
                if (not (i == x and j == y)) and arr[i][j] == '*':
                    total += 1
 
    return total == k
 
 
def minesweep(n, m, grid):
    for i in range(n):
        for j in range(m):
            if grid[i][j] != '*':
                num = int(grid[i][j]) if grid[i][j] != '.' else 0
                if not k_neighbors(num, grid, i, j):
                    return False
    return True
 
res = minesweep(n, m, grid)
 
if res:
    print("YES")
else:
    print("NO")

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    string a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    
    int x=-1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]== '*')
                continue;
            else if(a[i][j] == '.' )
            {
                if(a[i][j+1]=='*' or a[i][j-1]=='*' or a[i-1][j]=='*' or a[i+1][j]=='*' or a[i-1][j-1]=='*' or a[i-1][j+1]=='*' or a[i+1][j+1]=='*' or a[i+1][j-1]=='*')
                {
                    cout<<"NO"<<endl;
                    x=1;
                    break;

                }
                else    
                    continue;
            }
            else
            {
                char k= a[i][j];
                int b=0;
                int p=(i==0)?i:i-1;
                while(p<=i+1)
                {
                    int q=(j==0)?j:j-1;
                    while(q<=j+1)
                    {
                        if(a[p][q]=='*')
                        {
                            b++;
                        }
                        q++;
                    }
                    p++;
                }
                if((char)(b+48)!=k)
                {
                    cout<<"NO"<<endl;
                    x=1;
                    break;
                }
                else
                    continue;
            }
        }
        if(x==1)
            break;
    }
    if(x==-1)
    cout<<"YES"<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle